uniform sampler2D defGeometry;
uniform sampler2D defCommon;
uniform sampler2D defExtend;
uniform vec3 lightDirection;
uniform vec3 lightPosition;
uniform vec4 lightColor;
uniform float lightCutoff;
uniform float lightFalloff;
varying vec2 texcoord0;
varying vec3 farRay;

void main()
{
    vec4 geo=texture2D(defGeometry,texcoord0);
	vec4 common=texture2D(defCommon,texcoord0);
	vec4 extend=texture2D(defExtend,texcoord0);
	
	vec3 nor=geo.xyz;
    vec3 pos=geo.a*farRay;
	
    vec3 lightDir=normalize(lightPosition-pos);
	float cutoff=max(0.0,dot(-lightDir,lightDirection));
	if(cutoff>lightCutoff){
	    cutoff=pow(cutoff,lightFalloff);
	    float diff=max(0.0,dot(nor,lightDir));
		vec3 eyeDir=normalize(-pos);
	    vec3 reLight=normalize(reflect(lightDir,nor));
	    float spec=(pow(max(0.0,dot(reLight,-eyeDir)),common.a*255.0));
	    gl_FragColor=cutoff*common*diff*lightColor+vec4(1.0,1.0,1.0,1.0)*spec;
	    gl_FragColor.a=extend.r;
	}else{
	    gl_FragColor=vec4(0,0,0,1);
	}
}